package com.caifu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caifu.mapper.ZcOrderingMapper;
import com.caifu.mapper.ZcOrderingRecordDetailsMapper;
import com.caifu.mapper.ZcOrderingRecordsMapper;
import com.caifu.pojo.ZcOrdering;
import com.caifu.pojo.ZcOrderingRecordDetails;
import com.caifu.pojo.ZcOrderingRecords;
import com.caifu.service.ZcOrderingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订餐记录表 服务实现类
 * </p>
 *
 * @author lyf
 * @since 2020-11-09
 */
@Service
public class ZcOrderingServiceImpl extends ServiceImpl<ZcOrderingMapper, ZcOrdering> implements ZcOrderingService {

    @Autowired
    private ZcOrderingMapper orderingMapper;

    @Autowired
    private ZcOrderingRecordsMapper orderingRecordsMapper;

    @Autowired
    private ZcOrderingRecordDetailsMapper orderingRecordDetailsMapper;

    @Override
    @Transactional
    public List<Map<String, String>> getOrderList(int start, int pageSize, String name, List<String> orgs,String state, String month, String symbol) {
        return orderingMapper.getOrderList(start,pageSize,name,orgs,state,month,symbol);
    }

    @Override
    @Transactional
    public int getOrderListCount(String name, List<String> orgs,String state, String month, String symbol) {
        return orderingMapper.getOrderListCount(name,orgs,state,month,symbol);
    }

    @Override
    @Transactional
    public ZcOrdering getLately(String orgNo) {
        return orderingMapper.getLately(orgNo);
    }

    @Override
    @Transactional
    public List<Map<String, String>> getPlatformData(int start, int pageSize, String startDate, String endDate, String type, String orderStatus,List<String> orgList) {
        return orderingMapper.getPlatformData(start,pageSize,startDate,endDate,type,orderStatus,orgList);
    }

    @Override
    @Transactional
    public int getPlatformDataCount(String startDate, String endDate, String type, String orderStatus,List<String> orgList) {
        return orderingMapper.getPlatformDataCount(startDate,endDate,type,orderStatus,orgList);
    }

    @Override
    public Map<String, String> getDetailsInfo(String id, String month, String day, String type) {
        return orderingMapper.getDetailsInfo(id,month,day,type);
    }

    @Override
    public Map<String, String> getDetailsFixedPoint(String id, String month, String day, String type) {
        return orderingMapper.getDetailsFixedPoint(id,month,day,type);
    }

    @Override
    public List<Map<String, String>> getDetailsToDoor(String id, String month, String day, String type) {
        return orderingMapper.getDetailsToDoor(id,month,day,type);
    }

    @Override
    public List<Map<String, String>> getUserOrg(int uerNo) {
        return orderingMapper.getUserOrg(uerNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDeliver(String name, List<String> orgs, String month,int userNo) {
        try{
            Date now = new Date();
            List<String> list = orderingMapper.getOrderList_two(name,orgs,"2",month);
            if(list != null && list.size() > 0){
                for(String s : list){
                    orderingMapper.update(null,new UpdateWrapper<ZcOrdering>().set("status","3").set("order_time",now).set("order_by",userNo).eq("id",s));
                }
                return "1";
            }else{
                return "2";
            }
        }catch (Exception e){
            e.printStackTrace();
            return "0";
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOrder(String id) {
        try{
            orderingMapper.delete(new QueryWrapper<ZcOrdering>().eq("id",id));
            List<ZcOrderingRecords> list =  orderingRecordsMapper.selectList(new QueryWrapper<ZcOrderingRecords>().eq("order_id",id));
            if(list != null && list.size() > 0){
                for(ZcOrderingRecords or : list) {
                    orderingRecordDetailsMapper.delete(new QueryWrapper<ZcOrderingRecordDetails>().eq("record_id", or.getId()));
                }
            }
            orderingRecordsMapper.delete(new QueryWrapper<ZcOrderingRecords>().eq("order_id",id));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional()
    public String getOrgNameById(String orgId) {
        return orderingMapper.getOrgNameById(orgId);
    }
}
